/**
  ******************************************************************************
  * @file       lscm_FSD_Encoder.h
  * @author     Ah_Da
  * @version    1.0
  * @date       8/6/2018
  * @brief    This ia a driver for FSD Encoder which aims to get every note's distance in 360 degree
	   
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LSCM_FSD_LIDAR_H
#define __LSCM_FSD_LIDAR_H

#include "stm32f4xx_hal.h"
#include "main.h"
#include <stdarg.h>
#include <stdio.h>
// define the RX DMA count size
#define USART1_DMA_IRQ_TRI  1

#define RX_BUF_SIZE   400
#define USART_TX_BUF_SIZE   128
#define CIRCULAR_BUF_COUNT 3
#define LIDAR_OPEN_CMD "#sf\r\n"  //Lidar star
#define LIDAR_STOP_CMD "#sf 0\r\n"
#define DATA_OFFSET 4 //Every node consists of 4 character
#define MOVINGAVE_COUNT 5  //Window size for the moving average filter
//typedef struct
//{
//    uint8_t rx_data;
//    uint8_t rx_index;
//    uint8_t rx_buffer[CIRCULAR_BUF_COUNT][RX_BUF_SIZE];
//    uint8_t rx_complete;
//    
//    uint8_t tx_buffer[USART_TX_BUF_SIZE];
//    uint8_t tx_complete;
//}Lidar_Uart_Buffer;

typedef struct{
int distance;
int Angle;
uint8_t error;
}FSDNode;

typedef struct{
int distance[MOVINGAVE_COUNT];
int count;
}MovingAveNode;

static unsigned char cbit[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8,
};


void lscm_Lidar_reset_buffer (int index);
void FSD_Lidar_Init(void);
void lscm_lidar_enqueue_data_to_buffer (uint8_t data);
void lscm_LIDAR_Open (void);
void lscm_LIDAR_Close (void);
unsigned char GetCrcPackage(unsigned char *buf);
uint8_t Data_Structure_Validation(unsigned char *buf);
uint8_t GetDataStartflag(uint8_t *Data);
void DisplayOverFlowNum(void);
void ShowAllObstacle(void);
void lscm_lidar_decode_buffer(void);
FSDNode GetFSDnode(uint8_t *buf);
int MovingAverageFilter(FSDNode node);
#endif /* __LSCM_FSD_ENCODER_H */
